package cn.com.customer.manager.ui.message.phone

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.com.customer.manager.Constants

import cn.com.customer.manager.R
import cn.com.customer.manager.adapter.PhoneAdapter
import cn.com.customer.manager.base.BaseFragment
import cn.com.customer.manager.bean.CallInfo
import cn.com.customer.manager.bean.CallList
import cn.com.customer.manager.bean.CallPlayInfo
import cn.com.customer.manager.bean.CallReceiveInfo
import com.chad.library.adapter.base.BaseQuickAdapter
import com.xuexiang.xutil.data.DateUtils
import com.xuexiang.xutil.tip.ToastUtils
import kotlinx.android.synthetic.main.fragment_phone.*

/**
 * A simple [Fragment] subclass.
 */
class PhoneFragment : BaseFragment(), BaseQuickAdapter.OnItemChildClickListener, BaseQuickAdapter.OnItemClickListener, BaseQuickAdapter.RequestLoadMoreListener, PhoneContract.View {
    private lateinit var mAdapter: PhoneAdapter
    private var page: Int = 0
    private var type: Int = 0
    private var requestParam: MutableMap<String, String> = mutableMapOf()
    private var mPresenter: PhoneContract.Presenter = PhonePresenter(this)
    override fun getLayoutId(): Int = R.layout.fragment_phone
    var list: MutableList<CallPlayInfo> = mutableListOf()
    override fun initView() {
        initRv()
        requestData()
        phone_tab.setDefaultSelection(0)
        phone_tab.setOnTabSelectionChangedListener { title, value ->
            type = if (title == "去电") {
                0
            } else {
                1
            }
        }
    }

    private fun initRv() {
        phone_Recycler.layoutManager = LinearLayoutManager(mContext, RecyclerView.VERTICAL, false)
        mAdapter = PhoneAdapter(R.layout.item_message_list)
        phone_Recycler.adapter = mAdapter
        mAdapter.onItemClickListener = this
        mAdapter.onItemChildClickListener = this
        mAdapter.setOnLoadMoreListener(this, phone_Recycler)
    }

    override fun onLoadMoreRequested() {
        page++
        requestData()
    }

    override fun onItemChildClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
    }

    companion object {
        @JvmStatic
        fun newInstance() = PhoneFragment().apply {
            arguments = Bundle().apply {
            }
        }
    }

    private fun requestData() {
        requestParam["pageNo"]=page.toString()
        requestParam["pageSize"]= Constants.PAGE_SIZE.toString()
        requestParam["beginDate"]=""
        requestParam["endDate"]= DateUtils.getNowString(DateUtils.yyyyMMdd.get())
        mPresenter.getCallList(requestParam)

    }

    override fun setCallList(result: CallList) {
        if (type == 0) {
            result.receivedList.forEach {
                list.add(CallPlayInfo(it, null))
            }
        } else {
            result.sendList.forEach {
                list.add(CallPlayInfo(null, it))
            }
        }
        if (page == 1) {
            mAdapter.setNewData(list)
        } else {
            mAdapter.addData(list)
        }
        if (mAdapter.isLoadMoreEnable and mAdapter.isLoading) {
            mAdapter.loadMoreComplete()
        }
        if(result.receivedList.size+result.sendList.size<Constants.PAGE_SIZE){
            mAdapter.loadMoreEnd()
        }
    }

    override fun setPresenter(presenter: PhoneContract.Presenter) {
        mPresenter = presenter
    }
}
